package vn.sms.vsms;

import vn.sms.vsms.data.ServiceItem;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class ListMenuAdapter extends BaseExpandableListAdapter {
	protected ServiceItem root;
	protected Context mContext;
	
	
	public ListMenuAdapter(Context mContext,ServiceItem root) {
		super();
		this.root = root;
		this.mContext = mContext;
	}

	@Override
	public Object getChild(int groupPosition, int childPosititon) {
		// TODO Auto-generated method stub
		return this.root.getChilds().get(groupPosition).getChilds().get(childPosititon);
	}

	@Override
	public long getChildId(int groupPosition, int childPosititon) {
		return this.root.getChilds().get(groupPosition).getChilds().get(childPosititon).getId();
	}

	@Override
	public View getChildView(int groupPosition, final int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
			ServiceItem item = ((ServiceItem) getChild(groupPosition, childPosition));
			String childText = item.getName();
			 
	        if (convertView == null) {
	            LayoutInflater infalInflater = (LayoutInflater) this.mContext
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = infalInflater.inflate(R.layout.list_item, null);	           
	        }
	        
	       
	         
	        TextView txtListChild = (TextView) convertView
	                .findViewById(R.id.lblListItem);
	        if(item.getMoney()> 0 )
	        {
	        	childText = childText + "(" + item.getMoney() + ")";
	        }
	        
	        txtListChild.setText(childText);
	        return convertView;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if(this.root.getChilds() != null &&  this.root.getChilds().get(groupPosition).getChilds() != null)
			return this.root.getChilds().get(groupPosition).getChilds().size();
		else return 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return this.root.getChilds().get(groupPosition);
	}

	@Override
	public int getGroupCount() {
		if(this.root.getChilds() != null)
			return this.root.getChilds().size();
		return 0;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return this.root.getChilds().get(groupPosition).getId();
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
		
		ServiceItem item = ((ServiceItem) getGroup(groupPosition));
		
		String headerTitle = item.getName() ;
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_group, null);           
        }
        convertView.setFocusable(false);
        if(item.getChilds() != null)
        	Log.i("logging",item.getName() + ":::" + item.getChilds().size());
         
        
        Button btn =(Button) convertView
                .findViewById(R.id.btn);
        btn.setText(String.valueOf(groupPosition + 1));
        btn.setTextColor(Color.parseColor("white"));		
        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.lblListHeader);
        lblListHeader.setTypeface(null, Typeface.BOLD);
        ImageView indicator 	= (ImageView)convertView.findViewById(R.id.indicator);
        if(item.getMoney()> 0 )
        {
        	headerTitle = headerTitle + "(" + item.getMoney() + ")";
        }
        lblListHeader.setText(headerTitle);
        
        if (getChildrenCount(groupPosition) > 0) {
            if (isExpanded)
            {
            	indicator.setImageResource(R.drawable.list_btn_collapse);
            }
            else
            {
            	indicator.setImageResource(R.drawable.list_btn_expand);            	
            }
        }
        else
        	indicator.setImageDrawable(null);
        
        return convertView;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}
	
	
	
	
	
	@Override
	public boolean isChildSelectable(int arg0, int arg1) {
		return true;
	}
	
	

}
